package com.ezjie.ielts.module_read.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ezjie.ielts.R;
import com.ezjie.ielts.model.SubjectDetail;

public class ReadExamExpandAdapter implements ExpandableListAdapter {

	private Context mContext;
	private List<Map<String, Object>> mGrounDataList = new ArrayList<Map<String, Object>>();

	private int mGroupSelectedIndex = -1;
	private int mChildSelectedIndex = -1;
	private boolean isCacheAll;

	public ReadExamExpandAdapter(Context context) {
		mContext = context;
	}

	// 填入数据
	public void reset(List<SubjectDetail> datalists) {
		mGrounDataList.clear();

		Map<String, Object> map1 = new HashMap<String, Object>();
		List<String> lists = new ArrayList<String>();
		map1.put("title", mContext.getResources().getString(R.string.choose_subject_title));
		for (int i = 0; i < datalists.size(); i++) {
			lists.add(datalists.get(i).class_name+"("+datalists.get(i).num+")");
		}
		map1.put("list", lists);
		mGrounDataList.add(map1);

		Map<String, Object> map2 = new HashMap<String, Object>();
		List<String> lists2 = new ArrayList<String>();
		map2.put("title", mContext.getResources().getString(R.string.sort_subject_title));
		String[] strs = mContext.getResources().getStringArray(R.array.sort_subjects);
		for (String str : strs) {
			lists2.add(str);
		}
		map2.put("list", lists2);
		mGrounDataList.add(map2);
	}

	@Override
	public void registerDataSetObserver(DataSetObserver observer) {
	}

	@Override
	public void unregisterDataSetObserver(DataSetObserver observer) {
	}

	@Override
	public int getGroupCount() {
		return mGrounDataList.size();
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		List<String> sortList = (List<String>) mGrounDataList.get(groupPosition).get("list");
		return sortList.size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		if (mGrounDataList.size() > groupPosition) {
			String groupTitle = (String) mGrounDataList.get(groupPosition).get("title");
			return groupTitle;
		}
		return null;
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		if (mGrounDataList.size() > groupPosition) {
			List<Map<String, Object>> child = (List<Map<String, Object>>) mGrounDataList.get(groupPosition).get("list");
			if (child.size() > childPosition) {
				return child.get(childPosition);
			} else
				return null;
		}
		return null;
	}

	@Override
	public long getGroupId(int groupPosition) {
		return 0;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return 0;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
		TextView titleView = null;
		if (null == convertView) {
			convertView = LayoutInflater.from(mContext).inflate(R.layout.exadapter_readexam_layout, null);
			titleView = (TextView) convertView.findViewById(R.id.tv_reading_item_text);
		} else {
			titleView = (TextView) convertView.findViewById(R.id.tv_reading_item_text);
		}
		String groupTitle = "" + getGroup(groupPosition);
		titleView.setText(groupTitle);
		ImageView indicatorImage = (ImageView) convertView.findViewById(R.id.iv_group_indicator);
		if (isExpanded) {
			indicatorImage.setImageDrawable(mContext.getResources().getDrawable(R.drawable.expand_status1));
		} else {
			indicatorImage.setImageDrawable(mContext.getResources().getDrawable(R.drawable.expand_status2));
		}
		return convertView;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
			ViewGroup parent) {
		TextView titleView = null;
		if (null == convertView) {
			convertView = LayoutInflater.from(mContext).inflate(R.layout.exadapter_readexam_child_layout, null);
			titleView = (TextView) convertView.findViewById(R.id.tv_reading_item_text);
		} else {
			titleView = (TextView) convertView.findViewById(R.id.tv_reading_item_text);
		}
		String sortList = (String) getChild(groupPosition, childPosition);
		titleView.setText(sortList);
		ImageView childSelected = (ImageView) convertView.findViewById(R.id.iv_child_selected);
		if (mGroupSelectedIndex == groupPosition && mChildSelectedIndex == childPosition) {
			childSelected.setVisibility(View.VISIBLE);
		} else {
			childSelected.setVisibility(View.INVISIBLE);
		}
		return convertView;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		if (groupPosition == 3 && isCacheAll) {
			return false;
		}
		return true;
	}

	@Override
	public boolean areAllItemsEnabled() {
		return false;
	}

	@Override
	public boolean isEmpty() {
		return false;
	}

	@Override
	public void onGroupExpanded(int groupPosition) {

	}

	@Override
	public void onGroupCollapsed(int groupPosition) {

	}

	@Override
	public long getCombinedChildId(long groupId, long childId) {
		return 0;
	}

	@Override
	public long getCombinedGroupId(long groupId) {
		return 0;
	}

	public void refreshDataList(List<Map<String, Object>> data) {
		if (data != null) {
			mGrounDataList = data;
		}
	}

	public void setSelectedIndex(int groupPosition, int childPosition) {
		mGroupSelectedIndex = groupPosition;
		mChildSelectedIndex = childPosition;
	}

}
